import Vue from 'vue'
import VueRouter from 'vue-router'
// import { get } from 'http'
Vue.use(VueRouter)
const routes = [
  { path: '/login', name: 'login', component: () => import('@/views/login') },
  { path: '/home',
    name: 'home',
    component: () => import('@/views/home'),
    redirect: '/article',
    children: [
      { path: '/welcome', name: 'welcome', component: () => import('@/views/welcome') },
      { path: '/article', name: 'article', component: () => import('@/views/article') },
      { path: '/articleadd', name: 'articleadd', component: () => import('@/views/articleadd/index.vue') },
      { path: '/articledit/:aid', name: 'articledit', component: () => import('@/views/articledit') },
      { path: '/account', name: 'account', component: () => import('@/views/account') }
    ]
  }
]
const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  let userinfo = sessionStorage.getItem('userinfo')
  if (!userinfo && to.path !== '/login') {
    return next('/login')
  }
  next()
})
// 登录验证功能完成，利用的是守卫。
export default router
